package com.cb.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.eduservice.entity.vo.TeacherQuery;
import com.cb.utils.R;
import com.cb.eduservice.entity.EduTeacher;
import com.cb.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Cebb
 * @since 2020-06-19
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    private final EduTeacherService eduService;

    @Autowired
    public EduTeacherController(EduTeacherService eduService) {
        this.eduService = eduService;
    }

    @ApiOperation("所有讲师列表")
    @GetMapping("all")
    public R findAllTeacher() {
        List<EduTeacher> list = eduService.list(null);

        return R.ok().data("items", list);
    }

    @ApiOperation("根据Id删除讲师(逻辑删除)")
    @DeleteMapping("{id}")
    public R removeTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {

        boolean flag = eduService.removeById(id);
        if (flag)
            return R.ok();

        return R.error().message("讲师添加失败");
    }

    @ApiOperation("分页查询讲师信息")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @ApiParam(name = "current", value = "当前页面号", required = true)
            @PathVariable("current") long current,
            @ApiParam(name = "limit", value = "一页展示的数据量", required = true)
            @PathVariable("limit") long limit) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        IPage<EduTeacher> page = eduService.page(teacherPage, null);
        // 总记录数
        long total = page.getTotal();
        // 数据集合
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("条件组合查询(分页)")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页面号", required = true)
            @PathVariable("current") long current,
            @ApiParam(name = "limit", value = "一页展示的数据量", required = true)
            @PathVariable("limit") long limit,
            @ApiParam(name = "teacherQuery", value = "查询条件")
            @RequestBody(required = false) TeacherQuery teacherQuery
    ) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> page = eduService.page(teacherPage, queryWrapper);
        // 总记录数
        long total = page.getTotal();
        // 数据集合
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(
            @ApiParam(name = "teacher", value = "讲师信息", required = true)
            @RequestBody EduTeacher teacher) {

        boolean save = false;
        try {
            save = eduService.save(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (save)
            return R.ok().message("添加成功");

        return R.error().message("讲师添加失败");
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("{id}")
    public R findTeacherById(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable("id") String id) {
        EduTeacher teacher = eduService.getById(id);
        if (teacher == null)
            return R.error().message("查询的讲师不存在");

        return R.ok().data("teacher", teacher);
    }

    @ApiOperation("根据ID修改讲师信息")
    @PutMapping("{id}")
    public R updateTeacherById(
            @ApiParam(name="id",value = "讲师id",required = true)
            @PathVariable("id") String id,
            @ApiParam(name="teacher",value = "修改后的讲师信息",required = true)
            @RequestBody EduTeacher teacher){

        teacher.setId(id);
        boolean flag = eduService.updateById(teacher);

        if (flag)
            return R.ok().message("修改成功");

        return R.error().message("当前讲师不存在");
    }

}

